﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.CallPolices.Authorization;
using LeadThing.AbpZeroTemplate.CallPolices.Dtos;
using LeadThing.AbpZeroTemplate.DataExporting.Excel.EpPlus;
using Abp.Timing.Timezone;
using Abp.Runtime.Session;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.CallPolices
{
    /// <summary>
    /// 一键报警表的导出EXCEL功能的实现
    /// </summary>
    public class CallPoliceListExcelExporter : EpPlusExcelExporterBase, ICallPoliceListExcelExporter
    {

        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;


        /// <summary>
        /// 构造方法
        /// </summary>
        public CallPoliceListExcelExporter(ITimeZoneConverter timeZoneConverter, IAbpSession abpSession)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }



        /// <summary>
        /// 导出一键报警表到EXCEL文件
        /// <param name="callPoliceListDtos">导出信息的DTO</param>
        /// </summary>
        public FileDto ExportCallPoliceToFile(List<CallPoliceListDto> callPoliceListDtos)
        {


            var file = CreateExcelPackage("callPoliceList.xlsx", excelPackage =>
            {

                var sheet = excelPackage.Workbook.Worksheets.Add(L("CallPolice"));
                sheet.OutLineApplyStyle = true;

                AddHeader(
                    sheet,
                     L("FileID"),
                     L("CallType"),
                     L("Status"),
                     L("CreationTime")

                    );
                AddObjects(sheet, 2, callPoliceListDtos,

             _ => _.FileID,

             _ => _.CallTypeID,

             _ => _.Status,

        _ => _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId())
       );
                //写个时间转换的吧
                //var creationTimeColumn = sheet.Column(10);
                //creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd";

                for (var i = 1; i <= 4; i++)
                {
                    sheet.Column(i).AutoFit();
                }

            });
            return file;

        }
    }
}
